//
//  CZMainViewController.swift
//  GZWeibo007
//
//  Created by Apple on 15/12/15.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
 
class CZMainViewController: UITabBarController{

    /// 按钮点击事件
    func tweetClickTo() {
        let composeVC = CZToTweetViewController(controllerType: CZToTweetViewControllerType.Compose)

        presentViewController(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
    }

        override func viewDidLoad() {
        super.viewDidLoad()
        
        // 自定义tabbar,是只读的,不能直接使用 = 进行赋值
        // KVC 进行替换
        let mainTabBar = CZMainTabbar()
        setValue(mainTabBar, forKey: "tabBar")
            mainTabBar.composeButton.addTarget(self, action: "tweetClickTo", forControlEvents: UIControlEvents.TouchUpInside)

            // 设置tabbar的tintColor
        self.tabBar.tintColor = UIColor.orangeColor()

        // 首页
        let homeVC = CZHomeViewController()
//        self.addChildVC(homeVC, title: "首页", imageName: "tabbar_home")
        self.addChildVC(homeVC, title: "首页", imageName:"tabbar_home")
        
        // 消息
        let messageVC = CZMessageViewController()
        self.addChildVC(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 发现
        let discoveryVC = CZDiscoveryViewController()
        self.addChildVC(discoveryVC, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileVC = CZProfileViewController()
        self.addChildVC(profileVC, title: "我", imageName: "tabbar_profile")
                }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 设置按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
    }

    /// controller: 控制器
    /// title: 控制器显示的标题
    /// imageName: 在tabbar上面显示的图片名称
    private func addChildVC(controller: UIViewController, title: String, imageName: String) {
        // 包装导航控制器
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
        // 设置title,导航栏和tabBar上都有
        controller.title = title
        
        // 设置图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        // 设置高亮图片
        let highLightedName = imageName + "_highlighted"
        controller.tabBarItem.selectedImage = UIImage(named: highLightedName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
                // 设置文字颜色
        // NSForegroundColorAttributeName: 设置文字的前景颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
            }
   //  MARK: -懒加载
    /// 撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加到tabbar里面
        self.tabBar.addSubview(button)
        
        // 添加点击事件
        button.addTarget(self, action: "tweetClickTo", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

}

